package com.jast.business.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jast.business.domain.BigdataHbaseKv;
import com.jast.business.param.AvrisParam;
import com.jast.business.util.EncryptionDecryption;
import com.jast.common.core.controller.BaseController;
import com.jast.common.core.domain.AjaxResult;
import com.jast.common.utils.DateUtils;
import com.jast.common.utils.http.HttpUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jast
 * @description
 * @date 2023-03-08 09:21
 */
@Controller
@RequestMapping("/avris")
public class AvrisController  extends BaseController {
    @GetMapping("/tool")
    public String tool()
    {
        return "avris/tool";
    }

    @PostMapping("/query")
    @ResponseBody
    public Map query(AvrisParam avrisParam) throws Exception {
        String queryType = avrisParam.getQueryType();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        EncryptionDecryption des = new EncryptionDecryption();// 自定义密钥
        //查询条件
        String condition = avrisParam.getCondition();
        switch (queryType){
            case "0":
                String esResult = queryES(condition,"ads_user_profile");
                String decrypt = des.decrypt(esResult);
                if(JSONUtil.isTypeJSON(decrypt)) {
                    stringStringHashMap.put("data", JSONUtil.toJsonPrettyStr(decrypt));
                }else{
                    stringStringHashMap.put("data", decrypt);
                }
                break;
            case "1":
                 esResult = queryES(condition,"lead_pool_info");
                 decrypt = des.decrypt(esResult);
                 if(JSONUtil.isTypeJSON(decrypt)) {
                     stringStringHashMap.put("data", JSONUtil.toJsonPrettyStr(decrypt));
                 }else{
                     stringStringHashMap.put("data", decrypt);
                 }
                break;
            case "2":
                String result = queryMySQL(condition);
                decrypt = des.decrypt(result);
                if(JSONUtil.isTypeJSON(decrypt)) {
                    stringStringHashMap.put("data", JSONUtil.toJsonPrettyStr(decrypt));
                }else{
                    stringStringHashMap.put("data", decrypt);
                }
                break;
            case "3":
                result = queryRedis(condition);
                decrypt = des.decrypt(result);
                if(JSONUtil.isTypeJSON(decrypt)) {
                    stringStringHashMap.put("data", JSONUtil.toJsonPrettyStr(decrypt));
                }else{
                    stringStringHashMap.put("data", decrypt);
                }
                break;
            case "4":
                result = queryClickhouse(condition);
                decrypt = des.decrypt(result);
                if(JSONUtil.isTypeJSON(decrypt)) {
                    stringStringHashMap.put("data", JSONUtil.toJsonPrettyStr(decrypt));
                }else{
                    stringStringHashMap.put("data", decrypt);
                }
                break;
        }
        return stringStringHashMap;

    }

    public static void main(String[] args) {
        System.out.println(queryClickhouse("{\n" +
                "    \"sql\":\"select * from ms_user_profile_d limit 10;\",\n" +
                "    \"url\":\"jdbc:clickhouse://10.86.16.47:8123/avris_main\",\n" +
                "    \"user\":\"sync2avris\",\n" +
                "    \"password\":\"sync_avris123\"\n" +
                "}"));
    }

    public static String queryClickhouse(String json){
        String url ="https://operations-center-web.faw-vw.com/vw-cdp/back/ck_data/by_sql";
        HttpRequest post = HttpUtil.createPost(url);
        post.header("appKey","avris");
        post.header("signature","2c49ce87b1f779d817c7c1e35a289474");
        post.header("timestamp","1663645002029");
        post.header("Content-Type","application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("host","172.16.24.143");
        jsonObject.put("port", 6379);
        jsonObject.put("password","Avris2@22#!");
        jsonObject.put("db",2);
        jsonObject.put("key","ruleActionScore_0efe23587e8044d29a680188158c7177");
        HttpRequest body = post.body(json);
        String result = body.execute().body();
        return result;
    }

    public static String queryRedis(String json){
        String url ="https://operations-center-web.faw-vw.com/vw-cdp/back/redis_data/by_key";
        HttpRequest post = HttpUtil.createPost(url);
        post.header("appKey","avris");
        post.header("signature","2c49ce87b1f779d817c7c1e35a289474");
        post.header("timestamp","1663645002029");
        post.header("Content-Type","application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("host","172.16.24.143");
        jsonObject.put("port", 6379);
        jsonObject.put("password","Avris2@22#!");
        jsonObject.put("db",2);
        jsonObject.put("key","ruleActionScore_0efe23587e8044d29a680188158c7177");
        HttpRequest body = post.body(json);
        String result = body.execute().body();
        return result;
    }

    public static String queryMySQL(String sql){
        String url ="https://operations-center-web.faw-vw.com/vw-cdp/back/mysql_data/by_sql";
        HttpRequest post = HttpUtil.createPost(url);
        post.header("appKey","avris");
        post.header("signature","2c49ce87b1f779d817c7c1e35a289474");
        post.header("timestamp","1663645002029");
        post.header("Content-Type","text/plain");
        HttpRequest body = post.body(sql);
        String result = body.execute().body();
        return result;
    }

    public static String queryES(String dsl,String index){
        String url ="https://operations-center-web.faw-vw.com/vw-cdp/back/es_data/by_dsl";
        HttpRequest post = HttpUtil.createPost(url);
        post.header("appKey","avris");
        post.header("signature","2c49ce87b1f779d817c7c1e35a289474");
        post.header("timestamp","1663645002029");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","get");
        jsonObject.put("dsl", JSONObject.parseObject(dsl));
        jsonObject.put("index",index);
        HttpRequest body = post.body(jsonObject.toJSONString());
        String result = body.execute().body();
        return result;
    }
}
